//
//  Layout.m
//  TestCar
//
//  Created by 吴志勇 on 2020/3/25.
//  Copyright © 2020 wzy. All rights reserved.
//

#import "Layout.h"

@implementation Layout

//什么时候调用: CollectionView第一次布局，CollectionView刷新的时候也会调用
//作用:计算Cell的布局,条件:cell的位置是固定不变
- (void)prepareLayout{
    [super prepareLayout];
}

//计算CollectionView滚动范围
- (CGSize)collectionViewContentSize{
    return [super collectionViewContentSize];
}


/**
 UICollectionViewLayoutAttributes: 确定cell的尺寸
 一个UICollectionViewLayoutAttributes就对应一个cell
 拿到UICollectionViewLayoutAttributes相当于拿到cell
 */
//返回很多Cell尺寸，作用:指定一段区域给你这段区域内cell的尺寸；
//可以一次性返回所有cell的尺寸
- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
//    NSArray *attrs =  [super layoutAttributesForElementsInRect:rect];
//    int i = 0;
//    for (UICollectionViewLayoutAttributes *attr in attrs) {
//        if (i==0) {
//            attr.transform = CGAffineTransformMakeScale(0.5, 0.5);
//        }
//        i++;
//    }
//    NSArray *attrs = [super layoutAttributesForElementsInRect:CGRectMake(0, 0, MAXFLOAT, MAXFLOAT)];
    
    //获取当前显示Cell布局
    NSArray *attrs = [super layoutAttributesForElementsInRect:self.collectionView.bounds];
    for (UICollectionViewLayoutAttributes *attr in attrs) {
        //计算中心点距离
        CGFloat distance = fabs(attr.center.x - self.collectionView.contentOffset.x - self.collectionView.bounds.size.width *0.5);
        CGFloat scale =1- distance / self.collectionView.bounds.size.width *0.5 *0.25;
    }
    
    
    return attrs;

}

//Invalidate:刷新
//在滚动的时候是否刷新布局
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
    return YES;
}

//用户手指一松开就会调用  确定最终偏移量
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity{
    //最终偏移量不等于 手指离开时偏移量
    //最终偏移量
   CGPoint targetP =  [super targetContentOffsetForProposedContentOffset:proposedContentOffset withScrollingVelocity:velocity];
    //获取CollectionView偏移量
//    self.collectionView.contentOffset;
    
    //    定位:距离中心点跃进，这个cell最终h展示到中心点
    //0.获取终于中显示的区域
    CGFloat collectW = self.collectionView.bounds.size.width;
    CGRect rect = CGRectMake(targetP.x, 0, collectW, MAXFLOAT);
    NSArray *attrs = [super layoutAttributesForElementsInRect:rect];
     CGFloat mixd = MAXFLOAT;
    for (UICollectionViewLayoutAttributes *attr in attrs) {
       
        CGFloat d = attr.center.x - self.collectionView.contentOffset.x - collectW *0.5;
        if (fabs(d) < fabs(mixd)) {
            mixd = d;
        }
    }
    targetP.x += mixd;
    
    if (targetP.x<0) {
        targetP.x = 0;
    }
    
    return targetP;
    
}




//- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect; // return an array layout attributes instances for all the views in the given rect
//- (nullable UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;
//- (nullable UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath;
//- (nullable UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString*)elementKind atIndexPath:(NSIndexPath *)indexPath;
//
//- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds; // return YES to cause the collection view to requery the layout for geometry information
//- (UICollectionViewLayoutInvalidationContext *)invalidationContextForBoundsChange:(CGRect)newBounds NS_AVAILABLE_IOS(7_0);
//
//- (BOOL)shouldInvalidateLayoutForPreferredLayoutAttributes:(UICollectionViewLayoutAttributes *)preferredAttributes withOriginalAttributes:(UICollectionViewLayoutAttributes *)originalAttributes NS_AVAILABLE_IOS(8_0);
//- (UICollectionViewLayoutInvalidationContext *)invalidationContextForPreferredLayoutAttributes:(UICollectionViewLayoutAttributes *)preferredAttributes withOriginalAttributes:(UICollectionViewLayoutAttributes *)originalAttributes NS_AVAILABLE_IOS(8_0);
//
//- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity; // return a point at which to rest after scrolling - for layouts that want snap-to-point scrolling behavior
//- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset NS_AVAILABLE_IOS(7_0); // a layout can return the content offset to be applied during transition or update animations
//
//#if UIKIT_DEFINE_AS_PROPERTIES
//@property(nonatomic, readonly) CGSize collectionViewContentSize; // Subclasses must override this method and use it to return the width and height of the collection view’s content. These values represent the width and height of all the content, not just the content that is currently visible. The collection view uses this information to configure its own content size to facilitate scrolling.
//#else
//- (CGSize)collectionViewContentSize; // Subclasses must override this method and use it to return the width and height of the collection view’s content. These values represent the width and height of all the content, not just the content that is currently visible. The collection view uses this information to configure its own content size to facilitate scrolling.


@end
